<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/02/xpath-functions">
    <xsl:param name="DOTNET2"/>
	
	<xsl:template match="Class"><xsl:variable name="Name" select="Name"/>
	<xsl:variable name="LName" select="LName"/>
        <xsl:variable name="HeaderInfo" select="HeaderInfo/ProjectName"/>
using System;
using System.Data;
using MySql.Data.MySqlClient;


namespace <xsl:value-of select="HeaderInfo/ProjectName"/>.DataObject  
{


	public class <xsl:value-of select="Name"/> : <xsl:value-of select="HeaderInfo/ProjectName"/>DataObject, IComparable 
	{
	
	
	
		
	
	
	
		public static <xsl:value-of select="Name"/>[] LoadUsingSQL ( string sql )
        {
            System.Collections.Generic.List&lt;<xsl:value-of select="Name"/>&gt; results = new System.Collections.Generic.List&lt;<xsl:value-of select="Name"/>&gt; ( );
            using ( IDbConnection sqlCon = new MySqlConnection ( GetDB ( ) ) )
            {
                sqlCon.Open ( );
                using ( IDbCommand command = sqlCon.CreateCommand ( ) )
				{
					command.CommandText = sql;

					using ( IDataReader reader = command.ExecuteReader ( ) )
					{
						while ( reader.Read ( ) )
						{
							results.Add ( Load ( Int64.Parse ( reader[ "id" ].ToString ( ) ) ) );
						}
					}
				}
            }
            return results.ToArray ( );
        }


        public static <xsl:value-of select="Name"/>[] LoadAll ( )
        {
            System.Collections.Generic.List&lt;<xsl:value-of select="Name"/>&gt; results = new System.Collections.Generic.List&lt;<xsl:value-of select="Name"/>&gt; ( );
            string sql = "select `id` from `<xsl:value-of select="LName"/>`";
            using ( IDbConnection sqlCon = new MySqlConnection ( GetDB ( ) ) )
            {
                sqlCon.Open ( );
                using ( IDbCommand command = sqlCon.CreateCommand ( ) )
				{
					command.CommandText = sql;

					using ( IDataReader reader = command.ExecuteReader ( ) )
					{
						while ( reader.Read ( ) )
						{
							results.Add ( Load ( Int64.Parse ( reader[ "id" ].ToString() ) ) );
						}
					}
				}
            }
            return results.ToArray ( );
        }
	
	
	
		public static <xsl:value-of select="Name"/> Load ( Int64 id )
		{
			string sql = "select * from `<xsl:value-of select="LName"/>` where `id`=" + id;
			
			using ( IDbConnection sqlCon = new MySqlConnection ( GetDB ( ) ) ) 
			{
				sqlCon.Open ();
				using ( IDbCommand command = sqlCon.CreateCommand ( ) )
				{
					command.CommandText = sql;
					
					using ( IDataReader reader = command.ExecuteReader ( ) )
					{
						if ( ! reader.Read ( ) )
						{
							throw new Exception ( "No data with id " + id );
						} else {
						
							<xsl:value-of select="Name"/> obj = new <xsl:value-of select="Name"/> ( );
							
							obj.ObjectID = Int64.Parse ( reader [ "id" ].ToString() );
							obj.CreatedDate = (DateTime ) reader [ "created" ];
							obj.ModifiedDate = (DateTime ) reader [ "modified" ];
							
							<xsl:for-each select="ClassAttribs/ClassAttributes">
								<xsl:choose>
									<xsl:when test="SqlSPType='string'">
										obj.<xsl:value-of select="Name"/> = (string)reader [ "<xsl:value-of select="Name"/>" ];
									</xsl:when>
									<xsl:when test="SqlSPType='int'">
										obj.<xsl:value-of select="Name"/> = int.Parse ( reader [ "<xsl:value-of select="Name"/>" ].ToString() );
									</xsl:when>
									<xsl:when test="SqlSPType='double'">
										obj.<xsl:value-of select="Name"/> = double.Parse ( reader [ "<xsl:value-of select="Name"/>" ].ToString() );
									</xsl:when>
									<xsl:when test="SqlSPType='DateTime'">
										obj.<xsl:value-of select="Name"/> = ( DateTime ) reader [ "<xsl:value-of select="Name"/>" ];
									</xsl:when>
									<xsl:when test="SqlSPType='Guid'">
										obj.<xsl:value-of select="Name"/> = ( Guid ) reader [ "<xsl:value-of select="Name"/>" ];
									</xsl:when>
								</xsl:choose>
							</xsl:for-each>
							
							
							return obj;
						
						}
					}
				}
				
				sqlCon.Close();
			}
		
			return null;
		}
	
	
	
		protected override bool insert ( IDbConnection sqlCon )
		{
			string sql = @"insert into `<xsl:value-of select="LName"/>` (
				<xsl:for-each select="ClassAttribs/ClassAttributes">`<xsl:value-of select="Name"/>`,</xsl:for-each>`Created`, `Modified`
			)
			values (
				<xsl:for-each select="ClassAttribs/ClassAttributes">
				
					<xsl:choose>
						<xsl:when test="SqlSPType='string' or SqlSPType='int' or SqlSPType='double'">
						'" + <xsl:value-of select="Name"/>.ToString().Replace ( "'","''" ) + @"',</xsl:when>
						
						<xsl:when test="SqlSPType='DateTime'">
						'" + <xsl:value-of select="Name"/>.ToString("yyyy-MM-dd HH:mm") + @"',
						</xsl:when>
						
						<xsl:when test="SqlSPType='Guid'">
						cast(0x" + <xsl:value-of select="Name"/>.ToString().Replace("-","") + @" as binary),
						</xsl:when>
						
						
					</xsl:choose>
				
				
				</xsl:for-each>
				'" + DateTime.Now.ToString ( "yyyy-MM-dd HH:mm" ) + "','" + DateTime.Now.ToString ( "yyyy-MM-dd HH:mm" ) + @"'
			)
			";
			
			
            using ( IDbCommand command = sqlCon.CreateCommand ( ) )
			{
				command.CommandType = CommandType.Text;
				command.CommandText = sql;
				command.ExecuteNonQuery ( );
			}
			
            using ( IDbCommand command = sqlCon.CreateCommand ( ) ) 
			{
				command.CommandText = "SELECT LAST_INSERT_ID() as id";
				using ( IDataReader reader = command.ExecuteReader ( ) )
				{
					reader.Read ( );
					this.ObjectID = (Int64)reader[ "id" ];
					reader.Close ( );
				}
			}

            return this.ObjectID != 0;
		}
		

		protected override bool update ( IDbConnection sqlCon )
		{
			string sql = @"update `<xsl:value-of select="LName"/>` SET
				<xsl:for-each select="ClassAttribs/ClassAttributes">
				`<xsl:value-of select="Name"/>`=
					<xsl:choose>
						<xsl:when test="SqlSPType='string' or SqlSPType='int' or SqlSPType='double'">
						'" + <xsl:value-of select="Name"/>.ToString().Replace ( "'","''" ) + @"',</xsl:when>
						<xsl:when test="SqlSPType='DateTime'">
						'" + <xsl:value-of select="Name"/>.ToString("yyyy-MM-dd HH:mm") + @"',
						</xsl:when>
						<xsl:when test="SqlSPType='Guid'">
						cast(0x" + <xsl:value-of select="Name"/>.ToString().Replace("-","") + @" as binary),
						</xsl:when>
					</xsl:choose>

				</xsl:for-each>
				`Modified`='" + DateTime.Now.ToString ( "yyyy-MM-dd HH:mm" ) + "' WHERE `id`=" + ObjectID + ";";
				
            using ( IDbCommand command = sqlCon.CreateCommand ( ) )
			{
				command.CommandType = CommandType.Text;
				command.CommandText = sql;
				command.ExecuteNonQuery ( );
			}
		
		
			return true;
		}
		
		
		
        public int CompareTo(object obj) {
            return ((<xsl:value-of select="Name"/>)obj).CreatedDate.CompareTo(CreatedDate);
        }  
		
		
		
		
		<xsl:for-each select="ClassAttribs/ClassAttributes">
			<xsl:choose>
				<xsl:when test="SqlSPType='string'">
		public static <xsl:value-of select="$Name"/>[] Get_<xsl:value-of select="PropertyName"/>Like ( string search )
		{
			return LoadUsingSQL ( "select `id` from <xsl:value-of select="$LName"/> where `<xsl:value-of select="Name"/>` like '%" + search + "%'" );
		}
		
		public static <xsl:value-of select="$Name"/>[] Get_<xsl:value-of select="PropertyName"/>Equals ( string search )
		{
			return LoadUsingSQL ( "select `id` from <xsl:value-of select="$LName"/> where `<xsl:value-of select="Name"/>` = '" + search + "'" );
		}
				</xsl:when>
				
				
				<xsl:when test="SqlSPType='int'">
		public static <xsl:value-of select="$Name"/>[] Get_<xsl:value-of select="PropertyName"/>Equals ( int value )
		{
			return LoadUsingSQL ( "select `id` from <xsl:value-of select="$LName"/> where `<xsl:value-of select="Name"/>` = '" + value + "'" );
		}
		
		public static <xsl:value-of select="$Name"/>[] Get_<xsl:value-of select="PropertyName"/>LessThan ( int value )
		{
			return LoadUsingSQL ( "select `id` from <xsl:value-of select="$LName"/> where `<xsl:value-of select="Name"/>` &lt; '" + value + "'" );
		}
		
		public static <xsl:value-of select="$Name"/>[] Get_<xsl:value-of select="PropertyName"/>GreaterThan ( int value )
		{
			return LoadUsingSQL ( "select `id` from <xsl:value-of select="$LName"/> where `<xsl:value-of select="Name"/>` &gt; '" + value + "'" );
		}
				</xsl:when>
			</xsl:choose>
		</xsl:for-each>
		
	
	
	
		<xsl:apply-templates select="ClassAttribs"/>
	
	
	
	}
	



}
		
		
	</xsl:template>
	
	
	<xsl:template match="ClassAttributes">
		<xsl:if test="CType='single' and SqlSPType!='DateTime'">
		protected <xsl:value-of select="SqlSPType"/><xsl:text> </xsl:text><xsl:value-of select="Name"/><xsl:if test="SqlSPType='DateTime'">=DateTime.Now</xsl:if><xsl:if test="KnownClass='true'">=new <xsl:value-of select="SqlSPType"/>()</xsl:if><xsl:if test="Type='string'">=""</xsl:if>;
		public <xsl:value-of select="SqlSPType"/><xsl:text> </xsl:text><xsl:value-of select="PropertyName"/> {
			get { return <xsl:value-of select="Name"/>; }
			set { base.dirty=true; this.<xsl:value-of select="Name"/>=value; }
		}
		</xsl:if>
        
        <xsl:if test="CType='single' and SqlSPType='DateTime'">
		protected <xsl:value-of select="SqlSPType"/><xsl:text> </xsl:text><xsl:value-of select="Name"/><xsl:if test="SqlSPType='DateTime'">=DateTime.Now</xsl:if><xsl:if test="KnownClass='true'">=new <xsl:value-of select="SqlSPType"/>()</xsl:if><xsl:if test="Type='string'">=""</xsl:if>;
		public <xsl:value-of select="SqlSPType"/><xsl:text> </xsl:text><xsl:value-of select="PropertyName"/> {
			get  { return <xsl:value-of select="Name"/>; } 
			set 
			{ 
				base.dirty=true; 
				<xsl:value-of select="Name"/> = value;
			}
                
		}
		</xsl:if>
		
	</xsl:template>
</xsl:stylesheet>